﻿using System;
using System.Runtime.Serialization;

namespace Ns.Data.Entity
{
    /// <summary>
    /// Represents a type supporting change tracking, change notification, identified with <see cref="System.Guid"/> and auditing creation information with <see cref="System.String"/> and <see cref="System.DateTimeOffset"/>.
    /// </summary>    
    /// <remarks>Storage members are marked as protected in order to enable mapping with LinqToSql.</remarks>
#if !SILVERLIGHT
    [Serializable]
#endif
    [DataContract]
    public class CreateEntity : CreateEntity<Guid, string, DateTimeOffset>
    {
        #region Constructors

        static CreateEntity()
        {
            ChangingId += (sender, e) =>
            {
                if (!e.Handled)
                    e.Result = Guid.NewGuid();
            };

#if !SILVERLIGHT
            ChangingCreatedBy += (sender, e) => 
            {
                if (!e.Handled) 
                    e.Result = System.Threading.Thread.CurrentPrincipal.Identity.Name; 
            };
#endif
            ChangingCreatedOn += (sender, e) =>
            {
                if (!e.Handled)
                    e.Result = DateTimeOffset.Now;
            };
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateEntity"/> class 
        /// with the <see cref="PersistEntity.CurrentPersistChange"/> 
        /// and according to the <see cref="PersistEntity.CurrentChangeTracking"/>.
        /// </summary>
        public CreateEntity()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateEntity"/> class 
        /// with the <see cref="PersistEntity.CurrentPersistChange"/> 
        /// and according to the specified <see cref="ChangeTracking"/>.
        /// </summary>
        /// <param name="changeTracking">The change tracking.</param>
        public CreateEntity(ChangeTracking changeTracking)
            : base(changeTracking)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateEntity"/> class 
        /// with the specified <see cref="PersistChange"/> 
        /// and according to the <see cref="PersistEntity.CurrentChangeTracking"/>.
        /// </summary>
        /// <param name="persistChange">The persist change.</param>
        public CreateEntity(PersistChange persistChange)
            : base(persistChange)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateEntity"/> class 
        /// with the specified <see cref="PersistChange"/> 
        /// and according to the specified <see cref="ChangeTracking"/>.
        /// </summary>
        /// <param name="persistChange">The persist change.</param>
        /// <param name="changeTracking">The change tracking.</param>
        public CreateEntity(PersistChange persistChange, ChangeTracking changeTracking)
            : base(persistChange, changeTracking)
        {
        }

        #endregion        
    }
}
